<?php
/**
 * Created by PhpStorm.
 * User: lwt
 * Date: 4/26/19
 * Time: 11:12 AM
 */

namespace app\index\controller;

use think\Request;
use app\index\model\DealerModel;

class Dealer
{
    /**
     * 添加經銷商
     * @return \think\response\Json
     */
    public function addDealer(){
        $request = Request::instance();
        $data = $request->post();
        $data["de_add_time"] = time();
        $code = DealerModel::addDealer($data);
        if ($code != 0){
            $content["code"] = 200;
            $content["msg"] = "註冊成功！";
            return json($content);
        }else{
            $content["code"] = 400;
            $content["msg"] = "用戶已存在！";
            return json($content);
        }
    }

    /**
     * 經銷商登錄
     * @return \think\response\Json
     */
    public function login(){
        $request = Request::instance();
        $data = $request->post();
        $code = DealerModel::login($data["de_openid"]);
        return json($code);

    }

    /**
     * 查询所有经销商
     * @return \think\response\Json
     */
    public function selectAllDealer(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $content = array();
        $data = DealerModel::selectAllDealer();
        $content["code"] = 200;
        $content["num"] = count($data);
        $content["msg"] = $data;
        return json($content);
    }

    /**
     * 模糊查詢经销商
     * @return \think\response\Json
     */
    public function selectLikeDealer(){
        $request = Request::instance();
        $data = $request->param();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $content = array();
        $data = DealerModel::selectLikeDealer($data["de_name"]);
        $content["code"] = 200;
        $content["num"] = count($data);
        $content["msg"] = $data;
        return json($content);
    }

    /**
     * 更改经销商状态
     * @return \think\response\Json
     */
    public function updateDealerType(){
        $request = Request::instance();
        $data = $request->param();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $content = array();
        $data = DealerModel::updateDealerType($data);
        $content["code"] = 200;
        $content["msg"] = "更改成功！";
        return json($content);
    }
}